package com.sanjiahantang.youjiarole.module.programmeprice.adapters;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sanjiahantang.plugin.swipemenulistview.NoRollSwipeMenuListView;
import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.uilibrary.views.MyListView;
import com.sanjiahantang.uilibrary.views.TagGroup;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.config.StaticValue;
import com.sanjiahantang.youjiarole.module.common.views.impl.WebActivity;
import com.sanjiahantang.youjiarole.module.goods.views.impl.GoodsListActivity;
import com.sanjiahantang.youjiarole.module.programmeprice.beans.ProgramBean;
import com.sanjiahantang.youjiarole.module.programmeprice.views.ProgramPriceView;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import static android.view.View.GONE;


/**
 * Created by zy on noall017/8/9.
 */

public class ProgramAdapter extends RecyclerView.Adapter implements StaticValue{

    private final Context context;
    //布局标识集合
    private final List<Integer> typeList;
    private LayoutInflater inflater;
    private ProgramBean.Data data;
    private BaseView.OnLoadListener loadListener;
    public String sivisible_swich;
    private  int jjposition,mposition;
    private HashMap<Integer,TextView> titleMap;
    public ProgramBean.Data getData() {
        return data;
    }

    public void setData(ProgramBean.Data data) {
        this.data = data;
    }

    public void addCell(Object item, int type, String issimlar) {
        switch (type) {
            case 2:
                ProgramBean.Data.GoodsListBean accessoriesListBean = (ProgramBean.Data.GoodsListBean) item;
                if (issimlar.equals("find")){
                    data.getAccessoriesList().remove(mposition);
                    int accessoryIndex = 0, max = data.getAccessoriesList().size();
                    for (; accessoryIndex < max; accessoryIndex++) {
                        if (data.getAccessoriesList().get(accessoryIndex).getSkuFaId().contains(accessoriesListBean.getSkuFaId())) {
                            data.getAccessoriesList().get(accessoryIndex).setGoodsNum(accessoriesListBean.getGoodsNum() + data.getAccessoriesList().get(accessoryIndex).getGoodsNum());
                            break;
                        }
                    }
                    if (accessoryIndex == max) {
                        if (data.getAccessoriesList() != null&&data.getAccessoriesList().size()!=0) {
                            accessoriesListBean.setPhone(data.getAccessoriesList().get(0).getPhone());
                        } else {
                            accessoriesListBean.setPhone("");
                        }
                        data.getAccessoriesList().add(accessoriesListBean);
                    }

                }else {
                    int accessoryIndex = 0, max = data.getAccessoriesList().size();
                    for (; accessoryIndex < max; accessoryIndex++) {
                        if (data.getAccessoriesList().get(accessoryIndex).getSkuFaId().contains(accessoriesListBean.getSkuFaId())) {
                            data.getAccessoriesList().get(accessoryIndex).setGoodsNum(accessoriesListBean.getGoodsNum() + data.getAccessoriesList().get(accessoryIndex).getGoodsNum());
                            data.getAccessoriesList().get(accessoryIndex).setGoodsCount(String.valueOf((accessoriesListBean.getGoodsNum() + data.getAccessoriesList().get(accessoryIndex).getGoodsNum()) * Float.valueOf(data.getAccessoriesList().get(accessoryIndex).getGoodsPrice())));
                            break;
                        }
                    }
                    if (accessoryIndex == max) {
                        if (data.getAccessoriesList() != null) {
                            accessoriesListBean.setPhone(data.getAccessoriesList().get(0).getPhone());
                        } else {
                            accessoriesListBean.setPhone("");
                        }
                        data.getAccessoriesList().add(accessoriesListBean);
                    }
                }

                break;
            case 1:
                ProgramBean.Data.GoodsListBean furnitureListBean = (ProgramBean.Data.GoodsListBean) item;
                if (issimlar.equals("find")){
                    data.getFurnitureList().remove(jjposition);
                    int funitureIndex = 0, maxfun = data.getFurnitureList().size();
                    for (; funitureIndex < maxfun; funitureIndex++) {
                        if (data.getFurnitureList().get(funitureIndex).getSkuFaId().contains(furnitureListBean.getSkuFaId())) {
                            data.getFurnitureList().get(funitureIndex).setGoodsNum(furnitureListBean.getGoodsNum() + data.getFurnitureList().get(funitureIndex).getGoodsNum());
                            data.getFurnitureList().get(funitureIndex).setGoodsCount(String.valueOf((data.getFurnitureList().get(funitureIndex).getGoodsNum()) * Float.valueOf(data.getFurnitureList().get(funitureIndex).getGoodsPrice())));
                            break;
                        }
                    }
                    if (funitureIndex == maxfun) {
                        if (data.getFurnitureList() != null&&data.getFurnitureList().size()!=0) {
                            furnitureListBean.setPhone(data.getFurnitureList().get(0).getPhone());
                        } else {
                            furnitureListBean.setPhone("");
                        }
                        data.getFurnitureList().add(furnitureListBean);
                    }
                    notifyDataSetChanged();
                }else {
                    int funitureIndex = 0, maxfun = data.getFurnitureList().size();
                    for (; funitureIndex < maxfun; funitureIndex++) {
                        if (data.getFurnitureList().get(funitureIndex).getSkuFaId().contains(furnitureListBean.getSkuFaId())) {
                            data.getFurnitureList().get(funitureIndex).setGoodsNum(data.getFurnitureList().get(funitureIndex).getGoodsNum());
                            data.getFurnitureList().get(funitureIndex).setGoodsCount(String.valueOf((data.getFurnitureList().get(funitureIndex).getGoodsNum()) * Float.valueOf(data.getFurnitureList().get(funitureIndex).getGoodsPrice())));
                            break;
                        }
                    }
                    if (funitureIndex == maxfun) {
                        if (data.getFurnitureList() != null) {
                            furnitureListBean.setPhone(data.getFurnitureList().get(0).getPhone());
                        } else {
                            furnitureListBean.setPhone("");
                        }
                        data.getFurnitureList().add(furnitureListBean);
                    }
                }
        }
        notifyDataSetChanged();
    }

    public void setChangeData(String isoo) {
        sivisible_swich = isoo;
        if ("1".equals(sivisible_swich)) {//编辑刷新
            notifyDataSetChanged();
        } else if (ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich)) {//全不选

            for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
            }
            for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                ProgramAdapter.this.data.getMaterialList().get(i).setIsRecommend(0);
            }

            for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                ProgramAdapter.this.data.getFurnitureList().get(i).setIsRecommend(0);
            }
            for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                ProgramAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(0);
            }
        } else if (ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {//全选
            for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
            }
            for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                ProgramAdapter.this.data.getMaterialList().get(i).setIsRecommend(1);
            }

            for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                ProgramAdapter.this.data.getFurnitureList().get(i).setIsRecommend(1);
            }
            for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                ProgramAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(1);
            }

        } else {
//            for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
//                ProgramAdapter.this.data.getServiceList().get(i).getIsRecommend();
//            }
//            for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
//                ProgramAdapter.this.data.getMaterialList().get(i).getIsRecommend();
//            }
//
//            for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
//                ProgramAdapter.this.data.getFurnitureList().get(i).getIsRecommend();
//            }
//            for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
//                ProgramAdapter.this.data.getAccessoriesList().get(i).getIsRecommend();
//            }

        }
        notifyDataSetChanged();
        TotalMoney();

    }






    public ProgramAdapter(Context context, BaseView.OnLoadListener listener) {
        this.context = context;
        this.typeList = new ArrayList<>();
        typeList.add(ProgramPriceView.TYPE_SERVICE);
        typeList.add(ProgramPriceView.TYPE_MATERIAL);
        typeList.add(ProgramPriceView.TYPE_FURNITURE);
        typeList.add(ProgramPriceView.TYPE_ACCESSORIES);
        this.loadListener = listener;
        this.inflater = LayoutInflater.from(context);
        titleMap = new HashMap<>();
    }

    @Override
    public ProgramHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        int resId = -1;
        switch (viewType) {
            case ProgramPriceView.TYPE_SERVICE:
                resId = R.layout.item_program_nopic;
                break;
            case ProgramPriceView.TYPE_MATERIAL:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case ProgramPriceView.TYPE_FURNITURE:
                resId = R.layout.item_program_nopic_swiplist;
                break;
            case ProgramPriceView.TYPE_ACCESSORIES:
                resId = R.layout.item_program_nopic_swiplist;
                break;

        }
        return new ProgramHolder(inflater.inflate(resId, null), data, viewType);

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        ((ProgramHolder) holder).bindData(data, typeList.get(position));
    }

    @Override
    public int getItemViewType(int position) {
        return typeList.get(position);
    }

    @Override
    public int getItemCount() {
        return typeList.size();
    }

    private class ProgramHolder extends RecyclerView.ViewHolder {

        public ProgramHolder(View itemView, ProgramBean.Data data, int position) {
            super(itemView);
            bindData(data, position);
        }

        private void bindData(ProgramBean.Data data, int position) {

            if (position == ProgramPriceView.TYPE_SERVICE) {
                //施工列表
                initService(itemView, data.getServiceList());
            }
            if (position == ProgramPriceView.TYPE_MATERIAL) {
                //主材列表
                initGoods(itemView,0,"主材", false,position,data.getMaterialList());
            }

            if (position == ProgramPriceView.TYPE_FURNITURE) {
                initGoods(itemView,1,"家具", true,position,data.getFurnitureList());
            }

            if (position == ProgramPriceView.TYPE_ACCESSORIES) {
                initGoods(itemView,2,"配饰", true,position,data.getAccessoriesList());
            }

        }

        //施工
        public void initService(View itemView, final List<ProgramBean.Data.ServiceListBean> sgData) {
            final CheckBox sg_checkbox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final MyListView goodListView = (MyListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            txtGoodsCount.setText("施工");
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            final ImageView image_titleIcon = (ImageView) itemView.findViewById(R.id.image_titleIcon);
            sg_checkbox.setEnabled(false);
            QuickAdapter SGAdapter = new QuickAdapter<ProgramBean.Data.ServiceListBean>(context, R.layout.item_program_nopic_n, sgData) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.ServiceListBean item) {
                    helper.setText(R.id.sg_name, item.getServiceName());
                    helper.setText(R.id.txtPrice, String.format("¥ %.2f" , Utils.toFloat(item.getServiceCount())));
                    ImageView inImageView = helper.getView(R.id.iv_pic);
                    String serviceSwich = "" + item.getIsRecommend();

                    if (!TextUtils.isEmpty(item.getRemark())){
                        //TODO 2018/3/20 新加施工点击事件
                        helper.setVisible(R.id.iv_arrow,true);
                        helper.getView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                WebActivity.open3DUrl(context,item.getRemark(),item.getServiceName());
                            }
                        });
                    }else{
                        helper.getView(R.id.iv_arrow).setVisibility(View.INVISIBLE);
                    }
                        switch (serviceSwich) {
                            case "0":
                                inImageView.setBackgroundResource(R.drawable.i_unselect_gray_circle_small);
                                sg_checkbox.setChecked(false);
                                for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
                                }
                                image_titleIcon.setVisibility(View.GONE);
                                sg_checkbox.setVisibility(View.VISIBLE);
                                break;
                            case "1":
                                image_titleIcon.setVisibility(View.VISIBLE);
                                sg_checkbox.setVisibility(GONE);
                                sg_checkbox.setChecked(true);

                                for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
                                }
                                inImageView.setBackgroundResource(R.drawable.i_select_gray_circle_small);
                                break;

                    }
                    if (ProgramPriceView.SWITCH_EDIT.equals(sivisible_swich) || ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich) || ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {
                        sg_checkbox.setEnabled(true);
                        image_titleIcon.setVisibility(View.GONE);
                        sg_checkbox.setVisibility(View.VISIBLE);
                    }
                    if (ProgramPriceView.SWITCH_VIEW.equals(sivisible_swich)) {
                        sg_checkbox.setVisibility(GONE);
                        inImageView.setVisibility(GONE);
                    }
                    //全选
                    sg_checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                            if (isChecked == false) {
                                sg_checkbox.setChecked(false);
                                for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(0);
                                }
                            } else {
                                sg_checkbox.setChecked(true);
                                for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                                    ProgramAdapter.this.data.getServiceList().get(i).setIsRecommend(1);
                                }

                            }
                            TotalMoney();
                            notifyDataSetChanged();

                        }
                    });
                }
            };
            TotalMoney();
            goodListView.setAdapter(SGAdapter);
        }


        //家居
        private void initGoods(View itemView,
                               final int index,
                               String title,
                               final boolean showAdd,
                               int goodsType,
                               final List<ProgramBean.Data.GoodsListBean> jjData) {
            final CheckBox checkBox = (CheckBox) itemView.findViewById(R.id.iv_titleIcon);
            final NoRollSwipeMenuListView listView = (NoRollSwipeMenuListView) itemView.findViewById(R.id.SG_list);
            TextView txtGoodsCount = (TextView) itemView.findViewById(R.id.comName);
            final View panel = itemView.findViewById(R.id.comAdd);
            final TextView line = (TextView) itemView.findViewById(R.id.line);
            txtGoodsCount.setText(title);
            titleMap.put(goodsType,txtGoodsCount);
            if (showAdd){
                panel.setVisibility(View.VISIBLE);
                //增项
                panel.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //添加家居项目
                        Intent intent = new Intent(context, GoodsListActivity.class);
                        intent.putExtra(KEY_TYPE_GOODSID, String.valueOf(index + 1));
                        context.startActivity(intent);
                    }
                });
            }else{
                panel.setVisibility(View.GONE);
            }

            if (ProgramPriceView.SWITCH_FIRST.equals(sivisible_swich)){
                panel.setVisibility(GONE);
            }

            if (ProgramPriceView.SWITCH_NO_EDIT.equals(sivisible_swich)){

            }else{

            }

            checkBox.setEnabled(false);
            final QuickAdapter adapter = new QuickAdapter<ProgramBean.Data.GoodsListBean>(context, R.layout.item_program_nopic_y) {
                @Override
                protected void convert(final BaseAdapterHelper helper, final ProgramBean.Data.GoodsListBean item) {
                    helper.setText(R.id.txtMainName, item.getGoodsName());
                    helper.setText(R.id.txtMainPrice, String.format("¥ %.2f" , Utils.toFloat(item.getGoodsPrice())));
                    helper.setText(R.id.txtCount, "" + item.getGoodsNum());
                    TagGroup tagGroup = helper.getView(R.id.txtName);
                    tagGroup.setTags(item.getBrandType());
                    final TextView tv_number = helper.getView(R.id.choice_number);
                    ImageView nearby_shop_img = helper.getView(R.id.iv_pic);
                    Picasso.with(context).load(item.getImageUrl()).placeholder(RES_ID_GOODS_DEFAULT).into(nearby_shop_img);
                    final CheckBox iv_icon = helper.getView(R.id.iv_icon);
                    iv_icon.setEnabled(false);
                    int isRecommend = item.getIsRecommend();
                    LinearLayout isFirst = helper.getView(R.id.isFirst);
                    LinearLayout isSecond = helper.getView(R.id.isSecond);
                    if (sivisible_swich.equals(ProgramPriceView.SWITCH_FIRST)) {
                        switch (isRecommend) {
                            case 0:
                                iv_icon.setChecked(false);
                                break;
                            case 1:
                                iv_icon.setChecked(true);
                                checkBox.setBackgroundResource(R.drawable.i_select_gray_circle);
                                iv_icon.setCompoundDrawablesWithIntrinsicBounds(context.getResources().getDrawable(R.drawable.i_select_gray_circle_small),null,null,null);
                                break;
                        }

                    }else {
                        switch (isRecommend) {
                            case 0:
                                iv_icon.setChecked(false);
                                break;
                            case 1:
                                iv_icon.setChecked(true);
                                checkBox.setBackgroundResource(R.drawable.i_radio_on);
                                break;
                        }

                    }
                    if (ProgramPriceView.SWITCH_EDIT.equals(sivisible_swich) || ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich) || ProgramPriceView.SWITCH_SELECT_ALL.equals(sivisible_swich)) {
                        iv_icon.setEnabled(true);
                        checkBox.setEnabled(true);
                        line.setVisibility(View.VISIBLE);
                        if (showAdd){
                            panel.setVisibility(View.VISIBLE);
                        }else{
                            panel.setVisibility(View.GONE);
                        }

                        isSecond.setVisibility(View.VISIBLE);
                        isFirst.setVisibility(GONE);
                        helper.setText(R.id.choice_number, "" + item.getGoodsNum());
                        notifyDataSetChanged();
                    }


                    if (ProgramPriceView.SWITCH_NO_EDIT.equals(sivisible_swich)) {
                        line.setVisibility(GONE);
                        panel.setVisibility(GONE);
                        isSecond.setVisibility(GONE);
                        isFirst.setVisibility(View.VISIBLE);
                        helper.setText(R.id.txtCount, String.valueOf(item.getGoodsNum()));
                    }
                    if (ProgramPriceView.SWITCH_SELECT_NONE.equals(sivisible_swich)) {
                        checkBox.setBackgroundResource(R.drawable.i_radio_off);
                    }

                    if (ProgramPriceView.SWITCH_VIEW.equals(sivisible_swich)) {//增项清单按钮gone
                        checkBox.setVisibility(GONE);
                        iv_icon.setVisibility(GONE);
                    }
                    //点击标题全选不全选
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            if (b == false) {
                                checkBox.setBackgroundResource(R.drawable.i_radio_off);
                                checkBox.setChecked(false);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramAdapter.this.data.getMaterialList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramAdapter.this.data.getFurnitureList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(0);
                                        }
                                        break;
                                }

                            } else {

                                checkBox.setBackgroundResource(R.drawable.i_radio_on);
                                checkBox.setChecked(true);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramAdapter.this.data.getMaterialList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramAdapter.this.data.getFurnitureList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramAdapter.this.data.getAccessoriesList().get(i).setIsRecommend(1);
                                        }
                                        break;
                                }

                            }
                            TotalMoney();
                            notifyDataSetChanged();
                        }
                    });
                    //减法
                    helper.getView(R.id.number_reduce).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            if (n > 1) {
                                n--;
                                tv_number.setText(String.valueOf(n));
                                switch (index){
                                    case 0:
                                        ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramAdapter.this.data.getMaterialList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));

                                        break;
                                    case 1:
                                        ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                        break;
                                    case 2:
                                        ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                        ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition())
                                                .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                        break;
                                }

                                TotalMoney();
                            } else {
                                Utils.showToast(context, "商品数量不能小于1");
                            }

                        }
                    });
                    //加法
                    helper.getView(R.id.number_add).setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            int n = Integer.valueOf(tv_number.getText().toString());
                            n++;
                            tv_number.setText(String.valueOf(n));
                            switch (index){
                                case 0:
                                    ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramAdapter.this.data.getMaterialList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                                case 1:
                                    ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                                case 2:
                                    ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setGoodsNum(Integer.parseInt(tv_number.getText().toString()));
                                    ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition())
                                            .setGoodsCount( String.valueOf(Float.valueOf(ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).getGoodsPrice()) * Integer.parseInt(tv_number.getText().toString())));
                                    break;
                            }

                            TotalMoney();
                        }
                    });

                    iv_icon.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            int count = 0;
                            if (b == false) {
                                iv_icon.setChecked(false);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramAdapter.this.data.getMaterialList().get(i).getIsRecommend();
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramAdapter.this.data.getFurnitureList().get(i).getIsRecommend();
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setIsRecommend(0);
                                            count += ProgramAdapter.this.data.getAccessoriesList().get(i).getIsRecommend();
                                        }
                                        break;
                                }

                                if (count == 0) {
                                    checkBox.setBackgroundResource(R.drawable.i_radio_off);
                                    checkBox.setChecked(false);
                                }
                            } else {
                                iv_icon.setChecked(true);
                                switch (index){
                                    case 0:
                                        for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                                            ProgramAdapter.this.data.getMaterialList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramAdapter.this.data.getMaterialList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramAdapter.this.data.getMaterialList().size()) {
                                            checkBox.setChecked(true);
                                        }
                                        break;
                                    case 1:
                                        for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                                            ProgramAdapter.this.data.getFurnitureList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramAdapter.this.data.getFurnitureList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramAdapter.this.data.getFurnitureList().size()) {
                                            checkBox.setChecked(true);
                                        }
                                        break;
                                    case 2:
                                        for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                                            ProgramAdapter.this.data.getAccessoriesList().get(helper.getPosition()).setIsRecommend(1);
                                            count += ProgramAdapter.this.data.getAccessoriesList().get(i).getIsRecommend();
                                        }
                                        if (count == ProgramAdapter.this.data.getAccessoriesList().size()) {
                                            checkBox.setChecked(true);
                                        }
                                        break;
                                }


                                if (count > 0) {
                                    checkBox.setBackgroundResource(R.drawable.i_radio_on);
                                }
                            }
                            TotalMoney();
                        }
                    });

                }
            };

            List<ProgramBean.Data.GoodsListBean> showList = new ArrayList<>();
            final TextView tvMore = (TextView) itemView.findViewById(R.id.tvMore);
            if (jjData != null){
                if (jjData.size() > SHOW_SIZE){
                    Object objSize = tvMore.getTag(R.id.decode);
                    if (objSize != null && objSize instanceof Integer){
                        int type = (int) objSize;
                        if (type == 1){
                            showList.addAll(jjData.subList(0,SHOW_SIZE));
                        }else{
                            showList.addAll(jjData);
                        }
                    }else{
                        showList.addAll(jjData.subList(0,SHOW_SIZE));
                        tvMore.setVisibility(View.VISIBLE);
                        tvMore.setTag(R.id.decode,1);
                    }

                    tvMore.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Object tag = view.getTag(R.id.decode);
                            if (tag != null && tag instanceof Integer){
                                int type = (int) tag;
                                if (type == 1){
                                    tvMore.setTag(R.id.decode,2);
                                    tvMore.setText("收起列表");
                                    adapter.clear();
                                    adapter.addAll(jjData);
                                }else{
                                    tvMore.setTag(R.id.decode,1);
                                    tvMore.setText("查看全部");
                                    adapter.clear();
                                    adapter.addAll(jjData.subList(0,SHOW_SIZE));
                                }
                            }
                        }
                    });
                }else{
                    showList.addAll(jjData);
                    tvMore.setVisibility(View.GONE);
                }

                adapter.clear();
                adapter.addAll(showList);
            }
            TotalMoney();
            listView.setAdapter(adapter);
        }

    }

    public void TotalMoney() {
        float sgPrice = 0;
        float zcPrice = 0;
        float jjPrice = 0;
        float psPrice = 0;
        List<ProgramBean.Data.ServiceListBean> serviceListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> materialListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> furnitureListBeen = new ArrayList<>();
        List<ProgramBean.Data.GoodsListBean> accessoriesListBeen = new ArrayList<>();
        //施工总金额
        if (ProgramAdapter.this.data.getServiceList() != null) {
            for (int i = 0; i < ProgramAdapter.this.data.getServiceList().size(); i++) {
                if (ProgramAdapter.this.data.getServiceList().get(i).getIsRecommend() == 1) {
                    sgPrice += Float.parseFloat(ProgramAdapter.this.data.getServiceList().get(i).getServicePrice()) *
                            Float.valueOf(ProgramAdapter.this.data.getServiceList().get(i).getServiceNum());
                    serviceListBeen.add(ProgramAdapter.this.data.getServiceList().get(i));
                }
            }
        }
        //资材总金额
        if (ProgramAdapter.this.data.getMaterialList() != null) {
            for (int i = 0; i < ProgramAdapter.this.data.getMaterialList().size(); i++) {
                if (ProgramAdapter.this.data.getMaterialList().get(i).getIsRecommend() == 1) {
                    zcPrice += Float.parseFloat(ProgramAdapter.this.data.getMaterialList().get(i).getGoodsPrice()) * Float.valueOf(ProgramAdapter.this.data.getMaterialList().get(i).getGoodsNum());
                    materialListBeen.add(ProgramAdapter.this.data.getMaterialList().get(i));
                }
            }
        }
        //家居总金额
        if (ProgramAdapter.this.data.getFurnitureList() != null) {
            for (int i = 0; i < ProgramAdapter.this.data.getFurnitureList().size(); i++) {
                if (ProgramAdapter.this.data.getFurnitureList().get(i).getIsRecommend() == 1) {
                    jjPrice += Float.parseFloat(ProgramAdapter.this.data.getFurnitureList().get(i).getGoodsPrice()) * Float.valueOf(ProgramAdapter.this.data.getFurnitureList().get(i).getGoodsNum());
                    furnitureListBeen.add(ProgramAdapter.this.data.getFurnitureList().get(i));
                }
            }
        }
        //配飾总金额
        if (ProgramAdapter.this.data.getAccessoriesList() != null) {
            for (int i = 0; i < ProgramAdapter.this.data.getAccessoriesList().size(); i++) {
                if (ProgramAdapter.this.data.getAccessoriesList().get(i).getIsRecommend() == 1) {
                    psPrice += Float.parseFloat(ProgramAdapter.this.data.getAccessoriesList().get(i).getGoodsPrice()) * Float.valueOf(ProgramAdapter.this.data.getAccessoriesList().get(i).getGoodsNum());
                    accessoriesListBeen.add(ProgramAdapter.this.data.getAccessoriesList().get(i));
                }
            }
        }
        int count = accessoriesListBeen.size() + furnitureListBeen.size() + materialListBeen.size() + serviceListBeen.size();
        int totalCount = data.getServiceList().size() + data.getAccessoriesList().size() + data.getFurnitureList().size() + data.getMaterialList().size();
        if (count > 0 && count == totalCount) {
            Object[] all = new Object[2];
            all[0] = CALLBACK_TYPE_BOOLEAN;
            all[1] = true;
            loadListener.onLoad(all);

        } else {
            Object[] noall = new Object[2];
            noall[0] = CALLBACK_TYPE_BOOLEAN;
            noall[1] = false;
            loadListener.onLoad(noall);
        }
        Object[] objects = new Object[2];
        objects[0] = CALLBACK_TYPE_QUICK_ADAPTER;
        objects[1] = sgPrice + zcPrice + jjPrice + psPrice;
        loadListener.onLoad(objects);

        if (titleMap.containsKey(ProgramPriceView.TYPE_MATERIAL))
            titleMap.get(ProgramPriceView.TYPE_MATERIAL).setText(String.format("主材 总价: %.2f (不包含管理费)",zcPrice));
        if (titleMap.containsKey(ProgramPriceView.TYPE_FURNITURE))
            titleMap.get(ProgramPriceView.TYPE_FURNITURE).setText(String.format("家居 总价: %.2f",jjPrice));
        if (titleMap.containsKey(ProgramPriceView.TYPE_ACCESSORIES))
            titleMap.get(ProgramPriceView.TYPE_ACCESSORIES).setText(String.format("配饰 总价: %.2f",psPrice));
    }


}
